<?php


namespace api\modules\v1\controllers\buss;

use common\models\base\CommonStatus;
use common\models\base\Provinces;
use services\base\CommonStatusService;
use services\base\ProvincesService;
use services\buss\AccountsConsumeService;
use services\buss\AccountsFansService;
use services\buss\DevicesService;
use services\buss\DoctorsService;
use services\buss\PlatformService;
use services\member\MemberService;
use Yii;
use api\controllers\OnAuthController;
use common\helpers\CommonHelper;
use services\buss\AccountsService;

class AccountsController extends OnAuthController
{
    public $modelClass = '';

    /**
     * @api {get} /v1/buss/accounts/index 账号列表
     * @apiGroup Accounts
     * @apiName index
     * @apiUse CommonParam
     *
     * @apiParam {String} keywords                  搜索词
     * @apiParam {int} project_id                   项目ID
     * @apiParam {int} platform_id                  平台ID
     * @apiParam {int} doctor_id                    医生ID
     * @apiParam {int} use_mid                      运营人ID
     * @apiParam {int} is_stream                    是否投流【1：是 0：否】
     * @apiParam {int} status                       状态
     * @apiParam {int} page
     * @apiParam {int} pageSize
     *
     * @apiSuccess {Object}  data
     * @apiSuccess {Array}   data.list                              列表数据
     * @apiSuccess {Number}  data.list.id                           ID
     * @apiSuccess {String}  data.list.name                         昵称【账号】
     * @apiSuccess {Number}  data.list.accounts_id                  系统账号ID
     * @apiSuccess {Number}  data.list.t_id                         账号类型ID
     * @apiSuccess {String}  data.list.t_name                       账号类型
     * @apiSuccess {String}  data.list.position                     人设定位
     * @apiSuccess {Number}  data.list.platform_id                  平台ID
     * @apiSuccess {String}  data.list.platform_name                平台
     * @apiSuccess {Number}  data.list.fans_num                     粉丝数
     * @apiSuccess {Number}  data.list.works_num                    作品数
     * @apiSuccess {Number}  data.list.created_member_id            创建人ID
     * @apiSuccess {String}  data.list.created_member_name          创建人
     * @apiSuccess {String}  data.list.change_date                  更新日期
     * @apiSuccess {String}  data.list.created_at_str               创建日期
     * @apiSuccess {String}  data.list.is_stream_str                是否投流
     * @apiSuccess {String}  data.list.doctor_name                  认证医生
     * @apiSuccess {String}  data.list.project_name                 项目
     * @apiSuccess {Number}  data.list.rate                         粉丝数变化数
     * @apiSuccess {String}  data.list.login_device_name            登录设备
     * @apiSuccess {String}  data.list.created_at
     * @apiSuccess {String}  data.list.updated_at
     * @apiSuccess {String}  data.condition
     * @apiSuccess {String}  data.condition.project_arr              项目筛选【{value:'', label:''}】
     * @apiSuccess {String}  data.condition.platform_arr             平台筛选
     * @apiSuccess {String}  data.condition.doctors_arr              认证医生筛选
     * @apiSuccess {String}  data.condition.principal_member_arr     运营人筛选
     * @apiSuccess {String}  data.condition.status_arr               状态筛选
     * @apiSuccess {String}  data.condition.account_type_arr         账号类型筛选
     * @apiSuccess {String}  data.condition.devices_arr              设备筛选
     * @apiSuccess {String}  data.condition.department_arr           部门筛选
     *
     */
    public function actionIndex(): array
    {
        $params = Yii::$app->request->get();

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $accountService = new AccountsService();
        $list = $accountService->getList($params, $page, $pageSize);

        // 筛选项
        $list['condition'] = [
            'project_arr' => MemberService::getProjectListCondition(),
            'platform_arr' => PlatformService::getListCondition(),
            'doctors_arr' => DoctorsService::getListCondition(),
            'status_arr' => AccountsService::getStatusCondition(),
            'principal_member_arr' => AccountsService::getPrincipalMemberCondition(),
            'account_type_arr' => CommonStatusService::getListCondition(CommonStatus::TYPE_ACCOUNTS),
            'devices_arr' => DevicesService::getListCondition(),
            'department_arr' => MemberService::getMemberDepartmentCondition()['list'],
        ];
        return $list;
    }

    /**
     * @api {get} /v1/buss/accounts/info 账号详情
     * @apiName info
     * @apiGroup Accounts
     * @apiUse CommonParam
     *
     * @apiParam {Number} id 账号ID【新增ID不传，编辑必传ID】
     *
     * @apiSuccess {Object} data                    返回数据
     * @apiSuccess {Number} data.id                 ID
     * @apiSuccess {Number} data.project_id         项目ID
     * @apiSuccess {Number} data.platform_id        平台ID
     * @apiSuccess {String} data.nickname           昵称
     * @apiSuccess {Number} data.t_id               账号类型ID
     * @apiSuccess {Number} data.doctor_id          医生ID
     * @apiSuccess {Number} data.login_device_id    设备ID
     * @apiSuccess {Number} data.is_stream          是否投流
     * @apiSuccess {Number} data.login_account      登陆账号
     * @apiSuccess {Number} data.login_passwd       登陆密码
     * @apiSuccess {Number} data.use_mid       运营人ID
     * @apiSuccess {String} data.accounts_reg_date  注册日期
     * @apiSuccess {Number} data.status             状态
     * @apiSuccess {Object} data.condition          筛选条件
     * @apiSuccess {Array}  data.condition.project              项目筛选【{value:'', label:''}】
     * @apiSuccess {Array}  data.condition.platform             平台筛选
     * @apiSuccess {Array}  data.condition.doctor              认证医生筛选
     * @apiSuccess {Array}  data.condition.principal_member     运营人筛选
     * @apiSuccess {Array}  data.condition.account_type         账号类型筛选
     * @apiSuccess {Array}  data.condition.device              设备筛选
     */
    public function actionInfo()
    {
        $params = Yii::$app->request->get();
        $id = $params['id'] ?? 0;

        // 查询结果
        $service = new AccountsService();
        return $service->getInfo((int)$id);
    }

    /**
     * @api {get} /v1/buss/accounts/get-list-by-project 根据项目获取医生，项目+部门获取运营人
     * @apiName get-list-by-project
     * @apiGroup Accounts
     * @apiUse CommonParam
     *
     * @apiParam {Number} project_id    项目ID
     * @apiParam {Number} department_id 部门ID
     *
     * @apiSuccess {Object} data                    返回数据
     * @apiSuccess {Object} data.principal_member   运营人
     * @apiSuccess {Object} data.doctor             医生
     * @apiSuccess {Object} data.department         部门
     */
    public function actionGetListByProject(): array
    {
        $params = Yii::$app->request->get();
        $params['project_id'] = isset($params['project_id']) && $params['project_id'] ? $params['project_id'] : null;
        $params['department_id'] = isset($params['department_id']) && $params['department_id'] ? $params['department_id'] : null;
        $condition['principal_member'] = MemberService::getMemberByProject($params['project_id'], $params['department_id']);
        $condition['doctor'] = [];
        if ($params['project_id']) {
            $condition['doctor'] = DoctorsService::getProjectEditCondition($params['project_id']);
        }
        return $condition;
    }

    /**
     * @api {post} /v1/buss/accounts/update 更新账号
     * @apiGroup Accounts
     * @apiName update
     * @apiUse CommonParam
     *
     * @apiBody {Number}   id                                               账号ID【编辑时必传】
     * @apiBody {String}   nickname                                         昵称
     * @apiBody {Number}   platform_id                                      平台ID
     * @apiBody {Number}   t_id                                             账号类型
     * @apiBody {Number}   doctor_id                                        认证医生ID
     * @apiBody {Number}   login_device_id                                  登录设备ID
     * @apiBody {Number}   is_stream                                        是否投流
     * @apiBody {String}   login_account                                    登录账号
     * @apiBody {String}   login_passwd                                     登录密码
     * @apiBody {Number}   use_mid                                     运营人ID
     * @apiBody {String}   accounts_reg_date                                注册日期
     * @apiBody {Number}   status                                           状态
     *
     * @apiSuccess {Number} data    新增/编辑ID
     */
    public function actionUpdate()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['nickname']) || !$params['nickname']) {
            return CommonHelper::api(422, '昵称必填');
        }
        if (!isset($params['platform_id']) || !$params['platform_id']) {
            return CommonHelper::api(422, '平台必填');
        }
        if (!isset($params['t_id']) || !$params['t_id']) {
            return CommonHelper::api(422, '账号类型必填');
        }
        if (!isset($params['is_stream']) || !is_numeric($params['is_stream'])) {
            return CommonHelper::api(422, '是否投流必填');
        }
//        if (!isset($params['login_account']) || !$params['login_account']) {
//            return CommonHelper::api(422, '登录手机号必填');
//        }
        if (!isset($params['accounts_reg_date']) || !$params['accounts_reg_date']) {
            return CommonHelper::api(422, '注册日期必填');
        }
        $params['id'] = $params['id'] ?? 0;
        $accountService = new AccountsService();
        return $accountService->updateAccounts($params);
    }

    /**
     * @throws \Exception
     * @api {post} /v1/buss/accounts/delete 删除账号
     * @apiGroup Accounts
     * @apiName delete
     * @apiUse CommonParam
     *
     * @apiBody {Number} id     ID
     *
     * @apiSuccess {Object} data    返回数据
     */
    public function actionDelete()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['id']) || !$params['id']) {
            return CommonHelper::api(422, '缺少数据ID');
        }

        $accountService = new AccountsService();
        return $accountService->deleteAccounts($params);
    }

    /**
     * @api {get} /v1/buss/accounts/get-fans-list 粉丝更新列表
     * @apiGroup Accounts
     * @apiName get-fans-list
     * @apiUse CommonParam
     *
     * @apiParam {int} id                           账号ID
     * @apiParam {int} page
     * @apiParam {int} pageSize
     *
     * @apiSuccess {Object}  data
     * @apiSuccess {Array}   data.list                              列表数据
     * @apiSuccess {Number}  data.list.id                           ID
     * @apiSuccess {String}  data.list.name                         昵称【账号】
     * @apiSuccess {Number}  data.list.accounts_id                  系统账号ID
     * @apiSuccess {String}  data.list.created_member_name          创建人
     * @apiSuccess {String}  data.list.created_at
     * @apiSuccess {String}  data.list.updated_at
     *
     */
    public function actionGetFansList(): array
    {
        $params = Yii::$app->request->get();

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $fansService = new AccountsFansService();
        return $fansService->getList($params, $page, $pageSize);
    }

    /**
     * @api {post} /v1/buss/accounts/update-fans 更新粉丝记录
     * @apiGroup Accounts
     * @apiName update-fans
     * @apiUse CommonParam
     *
     * @apiBody {Number} id             账号ID
     * @apiBody {Number} fans_num       粉丝数
     *
     * @apiSuccess {Object} data    返回数据
     */
    public function actionUpdateFans()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['id']) || !$params['id']) {
            return CommonHelper::api(422, '缺少数据ID');
        }
        if (!isset($params['fans_num']) || !is_numeric($params['fans_num']) || $params['fans_num'] <= 0) {
            return CommonHelper::api(422, '粉丝数必须为正整数');
        }

        $fansService = new AccountsFansService();
        return $fansService->updateAccountsFans($params);
    }

    /**
     * @api {post} /v1/buss/accounts/set-principal 批量设置运营
     * @apiGroup Accounts
     * @apiName set-principal
     * @apiUse CommonParam
     *
     * @apiBody {String} ids                账号ID【多个逗号隔开】
     * @apiBody {Number} project_id         项目ID
     * @apiBody {Number} department_id      部门ID
     * @apiBody {Number} use_mid       运营人ID
     *
     * @apiSuccess {Object} data    返回数据
     */
    public function actionSetPrincipal()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['ids']) || !$params['ids']) {
            return CommonHelper::api(422, '请选需要设置的账号');
        }
        if (!isset($params['department_id']) || !$params['department_id']) {
            return CommonHelper::api(422, '缺少部门ID');
        }
        if (!isset($params['use_mid']) || !$params['use_mid']) {
            return CommonHelper::api(422, '缺少运营人ID');
        }

        $accountService = new AccountsService();
        return $accountService->setPrincipal($params);
    }

    /**
     * @api {get} /v1/buss/accounts/consume 投流消费列表
     * @apiGroup Accounts
     * @apiName consume
     * @apiUse CommonParam
     *
     * @apiParam {int} id                           账号ID
     * @apiParam {String} date_stime                开始时间
     * @apiParam {String} date_etime                结束时间
     * @apiParam {int} page
     * @apiParam {int} pageSize
     *
     * @apiSuccess {Object}  data
     * @apiSuccess {Array}   data.list                              列表数据
     * @apiSuccess {Number}  data.list.account_id                   账号ID
     * @apiSuccess {String}  data.list.money                        金额
     * @apiSuccess {Number}  data.list.date_time_str                时间
     * @apiSuccess {Number}  data.list.city_name                    投放地区
     * @apiSuccess {Number}  data.list.hours                        时长
     * @apiSuccess {Number}  data.list.type_str                     投放类型
     * @apiSuccess {String}  data.list.hour_minute                  投放时间
     * @apiSuccess {String}  data.list.private_sms_num              有效私信
     * @apiSuccess {String}  data.list.effective_clue_num           有效线索
     * @apiSuccess {String}  data.list.give_mobile_num              留电数
     * @apiSuccess {String}  data.list.operation_name               操作人
     * @apiSuccess {String}  data.list.created_at
     * @apiSuccess {String}  data.list.updated_at
     * @apiSuccess {Object}  condition
     * @apiSuccess {Object}  condition.type                         投放类型
     * @apiSuccess {Object}  condition.city                         城市
     *
     */
    public function actionConsume(): array
    {
        $params = Yii::$app->request->get();

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $service = new AccountsConsumeService();
        $list = $service->consumeList($params, $page, $pageSize);
        $provinceService = new ProvincesService();
        $list['condition'] = [
            'type' => $service->getTypesCondition(),
            'city' => $provinceService->getCityCondition()
        ];
        return $list;
    }

    /**
     * @api {post} /v1/buss/accounts/create-consume 新增投流消费记录
     * @apiGroup Accounts
     * @apiName create-consume
     * @apiUse CommonParam
     *
     * @apiBody {Number} account_id                 账号ID
     * @apiBody {Number} money                      消费金额
     * @apiBody {String} date_time                  投流日期
     * @apiBody {String} city_id                    地区ID
     * @apiBody {String} hours                      时长（小时数）
     * @apiBody {String} type                       投放类型【1：涨粉数、2：主页浏览数 3：点赞评论 4：播放量】
     * @apiBody {String} hour_minute                投流时间【时分】
     * @apiBody {String} remark                     备注
     *
     * @apiSuccess {Object} data    返回数据
     */
    public function actionCreateConsume()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['account_id']) || !$params['account_id']) {
            return CommonHelper::api(422, '账号ID必传');
        }
        if (!isset($params['date_time']) || !$params['date_time']) {
            return CommonHelper::api(422, '消费时间必填');
        }
        if (!isset($params['money']) || !$params['money']) {
            return CommonHelper::api(422, '消费金额必填');
        }
        if (!isset($params['city_id']) || !$params['city_id']) {
            return CommonHelper::api(422, '投放地区必填');
        }
        if (!isset($params['hours']) || !$params['hours']) {
            return CommonHelper::api(422, '时长不能为空');
        }
        if (!isset($params['type']) || !$params['type']) {
            return CommonHelper::api(422, '投放类型不能为空');
        }
        if (!isset($params['hour_minute']) || !$params['hour_minute']) {
            return CommonHelper::api(422, '投放时间不能为空');
        }

        $service = new AccountsConsumeService();
        return $service->createConsume($params);
    }

    /**
     * @throws \Exception
     * @api {post} /v1/buss/accounts/update-consume 编辑投流消费记录
     * @apiGroup Accounts
     * @apiName update-consume
     * @apiUse CommonParam
     *
     * @apiBody {Number} account_id             账号ID
     * @apiBody {Number} money                  消费金额
     * @apiBody {String} date_time              消费时间
     * @apiBody {String} city_id                地区ID
     * @apiBody {String} hours                  时长（小时数）
     * @apiBody {String} type                   投放类型【1：涨粉数 2：主页浏览数 3：点赞评论 4：播放量】
     * @apiBody {String} hour_minute            投流时间【时分】
     * @apiBody {String} remark                 备注
     *
     * @apiSuccess {Object} data    返回数据
     */
    public function actionUpdateConsume()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['id']) || !$params['id']) {
            return CommonHelper::api(422, '编辑时ID必传');
        }
        if (!isset($params['account_id']) || !$params['account_id']) {
            return CommonHelper::api(422, '账号ID必传');
        }
        if (!isset($params['date_time']) || !$params['date_time']) {
            return CommonHelper::api(422, '消费时间必填');
        }
        if (!isset($params['money']) || !$params['money']) {
            return CommonHelper::api(422, '消费金额必填');
        }
        if (!isset($params['city_id']) || !$params['city_id']) {
            return CommonHelper::api(422, '地区必填');
        }
        if (!isset($params['hours']) || !$params['hours']) {
            return CommonHelper::api(422, '时长必填');
        }
        if (!isset($params['hour_minute']) || !$params['hour_minute']) {
            return CommonHelper::api(422, '投流时间必填');
        }

        $service = new AccountsConsumeService();
        return $service->updateConsume($params);
    }

    /**
     * @throws \Exception
     * @api {post} /v1/buss/accounts/update-consume-data 编辑投流消费记录数据
     * @apiGroup Accounts
     * @apiName update-consume-data
     * @apiUse CommonParam
     *
     * @apiBody {Number} consume_id             消费ID
     * @apiBody {Number} private_sms_num        有效私信
     * @apiBody {Number} effective_clue_num     有效线索
     * @apiBody {Number} give_mobile_num        留电数
     * @apiBody {Number} recommend_num          推荐数
     *
     * @apiSuccess {Object} data    返回数据
     */
    public function actionUpdateConsumeData()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['consume_id']) || !$params['consume_id']) {
            return CommonHelper::api(422, '消费ID必传');
        }

        $service = new AccountsConsumeService();
        return $service->updateConsumeData($params);
    }

    /**
     * @throws \Exception
     * @api {post} /v1/buss/accounts/delete-consume 删除消费
     * @apiGroup Accounts
     * @apiName delete-consume
     * @apiUse CommonParam
     *
     * @apiBody {Number} consume_id     消费ID
     *
     * @apiSuccess {Object} data    返回数据
     */
    public function actionDeleteConsume()
    {
        $consumeId = Yii::$app->request->post('consume_id');
        if (!$consumeId) {
            return CommonHelper::api(422, '缺少消费ID');
        }

        $service = new AccountsConsumeService();
        return $service->deleteConsume($consumeId);
    }
}
